package logic

import (
	"cart_srv/models"
	"context"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *cart.GoodsListRequest) (*cart.GoodsListResponse, error) {
	var g models.Goods
	goodslist, err := g.ListGoods()
	if err != nil {
		logx.Error("商品列表展示失败", err)
		return nil, err
	}
	var goodsList []*cart.GoodsList
	for _, goods := range goodslist {
		list := cart.GoodsList{
			Title:   goods.Title,
			Image:   goods.Image,
			Price:   float32(goods.Price),
			Num:     int64(goods.Num),
			GoodsId: int64(goods.ID),
		}
		goodsList = append(goodsList, &list)
	}

	return &cart.GoodsListResponse{
		List: goodsList,
	}, nil
}
